from typing import Any

from aiogram import F, types
from aiogram_dialog import DialogManager, Window
from aiogram_dialog.widgets.kbd import Back, Cancel, ScrollingGroup, Select
from aiogram_dialog.widgets.text import Format

from app.bot.dialog.dialog_states import SelectProxySG
from app.bot.dialog.getters.countries import countries_getter
from app.bot.dialog.widgets import LocaleText


async def set_country_id(
    call: types.CallbackQuery, widget: Any, manager: DialogManager, country_id: str
):

    manager.dialog_data["country_id"] = country_id
    await manager.switch_to(SelectProxySG.list_of_providers)


countries_window = Window(
    LocaleText("select-country"),
    ScrollingGroup(
        Select(
            Format("{item.name}"),
            "catselsel",
            lambda country: country.id,
            "countries",
            on_click=set_country_id,
        ),
        width=2,
        height=4,
        id="catsel",
        when=F["user_language"] == "ru",
    ),
    ScrollingGroup(
        Select(
            Format("{item.name_eng}"),
            "catselsel",
            lambda country: country.id,
            "countries",
            on_click=set_country_id,
        ),
        width=2,
        height=4,
        id="catsel",
        when=F["user_language"] == "en",
    ),
    Back(LocaleText("back-btn")),
    Cancel(LocaleText("cancel-btn")),
    state=SelectProxySG.list_of_countries,
    getter=countries_getter,
)
